/*
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Raúl Román López <rroman@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.Overlay;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Paint.Align;
import android.graphics.Shader.TileMode;

import com.LibreGeoSocial.Api.GeoNode;
import com.LibreGeoSocial.Api.Note;
import com.LibreGeoSocial.Api.Photo;
import com.LibreGeoSocial.Api.User;
import com.LibreGeoSocial.Api.Audio;
import com.LibreGeoSocial.App.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class GenericOverlay extends Overlay{
	private final int mRadius = 5;
	private static final int BITMAP_LENGTH = 30;
	private static final int NAME_MAX = 75;
	
	private GeoNode mNode;
	
	private String name = "";
	private Bitmap bitmap = null;
	private int color = -1;
	
	public GenericOverlay(Context context, GeoNode node){
		super();
		mNode = node;
		
		if(User.class.isInstance(node)){
			User user = (User)node;
			name = user.getUsername();
			Bitmap bm = user.getAvatarBitmap();
			if(bm == null)
				bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.user_48),
						BITMAP_LENGTH, BITMAP_LENGTH, false);
			else
				bitmap = Bitmap.createScaledBitmap(bm, BITMAP_LENGTH, BITMAP_LENGTH, false);
			color = Color.GRAY;
		}else if(Photo.class.isInstance(node)){
			Photo photo = (Photo)node;
			name = photo.getName();
			bitmap = Bitmap.createScaledBitmap(photo.getBitmapImageThumb(), BITMAP_LENGTH, BITMAP_LENGTH, false);
			color = Color.BLUE;
		}else if(Audio.class.isInstance(node)){
			Audio audio = (Audio)node;
			name = audio.getName();
			bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.audio_48),
					BITMAP_LENGTH, BITMAP_LENGTH, false);
			color = Color.RED;
		}else if(Note.class.isInstance(node)){
			Note note = (Note)node;
			name = note.getTitle();
			bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.paper_pencil_48),
					BITMAP_LENGTH, BITMAP_LENGTH, false);
			color = Color.YELLOW;
		}
			
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow)
	{
	
        GeoPoint geoPoint = new GeoPoint(  (int) (mNode.getLatitude() * 1000000), 
				  						   (int) (mNode.getLongitude() * 1000000));
        
		Point point= new Point();
		Projection projection = mapView.getProjection();
	        
		projection.toPixels(geoPoint, point);
	        
//		RectF oval = new RectF (point.x - mRadius, point.y - mRadius,
//								point.x + mRadius, point.y + mRadius);
	    
//		Paint paint = new Paint();
//		paint.setARGB(250,255,0,0);
//		paint.setShader(new RadialGradient(point.x, point.y, mRadius, Color.RED, Color.BLACK, TileMode.MIRROR));
//		paint.setAntiAlias(true);
//		paint.setFakeBoldText(true);
//		paint.setTextAlign(Align.CENTER);
		Paint paint = new Paint();
		paint.setColor(color);
		paint.setAntiAlias(true);
		paint.setFakeBoldText(true);
		paint.setTextAlign(Align.CENTER);
		
		Path arrow_path = new Path();
		arrow_path.moveTo(point.x - mRadius, point.y - mRadius);
		arrow_path.lineTo(point.x + mRadius, point.y - mRadius);
		arrow_path.lineTo(point.x, point.y);
		
		canvas.drawPath(arrow_path, paint); 
		
		Paint backPaint = new Paint();
		backPaint.setARGB(175,0,0,0);
		backPaint.setShader(new LinearGradient(point.x, point.y - (mRadius + 20), 
				point.x, point.y - mRadius, color, Color.BLACK, TileMode.MIRROR));
		backPaint.setAntiAlias(true);
	        
		String text = name;
		float tlongitude = paint.measureText(text);
		if(tlongitude > NAME_MAX){
			tlongitude = NAME_MAX;
			paint.setTextAlign(Align.LEFT);
		}
		
		RectF backRect = new RectF (point.x - tlongitude/2 - 10, point.y - (mRadius + 20),
				point.x + tlongitude/2 + 10, point.y - mRadius);
		
		Path path = new Path();
		path.moveTo(point.x - tlongitude/2, point.y - 10 - mRadius/2);
		path.lineTo(point.x + tlongitude/2, point.y - 10 - mRadius/2);
		
		canvas.drawRoundRect(backRect, 5, 5, backPaint);
		
		if(bitmap != null){
			int offset = BITMAP_LENGTH + 10;
			int x = (int) (point.x - offset / 2);
			int y = point.y - offset - (mRadius + 20);
			RectF avatarRect = new RectF (x, y,
					x + offset, y + offset);
			
			Paint avatarPaint = new Paint();
			avatarPaint.setARGB(175,0,0,0);
			avatarPaint.setShader(new LinearGradient(x, y, 
					x, y + offset, Color.WHITE, Color.DKGRAY, TileMode.MIRROR));
			avatarPaint.setAntiAlias(true);
			
			canvas.drawRoundRect(avatarRect, 5, 5, avatarPaint);
			canvas.drawBitmap(bitmap, x + 5, y + 5, null);
		}
		
//		canvas.drawText (text, point.x + 2*mRadius, point.y, paint);
		paint.setARGB(250,255,255,255);
		canvas.drawTextOnPath(text, path, 0, 0, paint);
	}
	
	
}